/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2009 - 2012 Luca Mingardi.
 *
 * This file is part of jeeObserver.
 *
 * JeeObserver is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * JeeObserver is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */
package jeeobserver;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;

/**
 * The Class JdbcPreparedStatement.
 *
 * @author Luca Mingardi
 * @version 4.0
 */
public class JdbcStatement implements Statement {

	private Statement realStatement;

	private String connectionUrl;

	private String currentQuery;

	public JdbcStatement(Statement realStatement, String connectionUrl) {
		this.realStatement = realStatement;
		this.connectionUrl = connectionUrl;
	}

	 @Override
	public ResultSet executeQuery(String sql) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		ResultSet result = this.realStatement.executeQuery(sql);
		observer.stop();

		return result;
	}

	 @Override
	public int executeUpdate(String sql) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		int result = this.realStatement.executeUpdate(sql);
		observer.stop();

		return result;
	}

	 @Override
	public int executeUpdate(String sql, int autoGeneratedKeys) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		int result = this.realStatement.executeUpdate(sql, autoGeneratedKeys);
		observer.stop();

		return result;
	}

	 @Override
	public int executeUpdate(String sql, int[] columnIndexes) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		int result = this.realStatement.executeUpdate(sql, columnIndexes);
		observer.stop();

		return result;
	}

	 @Override
	public int executeUpdate(String sql, String[] columnNames) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		int result = this.realStatement.executeUpdate(sql, columnNames);
		observer.stop();

		return result;
	}

	 @Override
	public boolean execute(String sql) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		boolean result = this.realStatement.execute(sql);
		observer.stop();

		return result;
	}

	 @Override
	public boolean execute(String sql, int autoGeneratedKeys) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		boolean result = this.realStatement.execute(sql, autoGeneratedKeys);
		observer.stop();

		return result;
	}

	 @Override
	public boolean execute(String sql, int[] columnIndexes) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		boolean result = this.realStatement.execute(sql, columnIndexes);
		observer.stop();

		return result;
	}

	 @Override
	public boolean execute(String sql, String[] columnNames) throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, sql);
		observer.start();
		boolean result = this.realStatement.execute(sql, columnNames);
		observer.stop();

		return result;
	}

	 @Override
	public int[] executeBatch() throws SQLException {
		ActionObserver observer = new ActionObserver(this.connectionUrl, this.currentQuery);
		observer.start();
		int[] result = this.realStatement.executeBatch();
		observer.stop();

		return result;
	}

	 @Override
	public void addBatch(String sql) throws SQLException {
		this.currentQuery = sql;
		this.realStatement.addBatch(sql);
	}

	 @Override
	public void clearBatch() throws SQLException {
		this.realStatement.clearBatch();
	}

	 @Override
	public void close() throws SQLException {
		this.realStatement.close();
	}

	 @Override
	public int getMaxFieldSize() throws SQLException {
		return this.realStatement.getMaxFieldSize();
	}

	 @Override
	public void setMaxFieldSize(int max) throws SQLException {
		this.realStatement.setMaxFieldSize(max);
	}

	 @Override
	public int getMaxRows() throws SQLException {
		return this.realStatement.getMaxRows();
	}

	 @Override
	public void setMaxRows(int max) throws SQLException {
		this.realStatement.setMaxRows(max);
	}

	 @Override
	public void setEscapeProcessing(boolean enable) throws SQLException {
		this.realStatement.setEscapeProcessing(enable);
	}

	 @Override
	public int getQueryTimeout() throws SQLException {
		return this.realStatement.getQueryTimeout();
	}

	 @Override
	public void setQueryTimeout(int seconds) throws SQLException {
		this.realStatement.setQueryTimeout(seconds);
	}

	 @Override
	public void cancel() throws SQLException {
		this.realStatement.cancel();
	}

	 @Override
	public SQLWarning getWarnings() throws SQLException {
		return this.realStatement.getWarnings();
	}

	 @Override
	public void clearWarnings() throws SQLException {
		this.realStatement.clearWarnings();
	}

	 @Override
	public void setCursorName(String name) throws SQLException {
		this.realStatement.setCursorName(name);
	}

	 @Override
	public ResultSet getResultSet() throws SQLException {
		return this.realStatement.getResultSet();
	}

	 @Override
	public int getUpdateCount() throws SQLException {
		return this.realStatement.getUpdateCount();
	}

	 @Override
	public boolean getMoreResults() throws SQLException {
		return this.realStatement.getMoreResults();
	}

	 @Override
	public void setFetchDirection(int direction) throws SQLException {
		this.realStatement.setFetchDirection(direction);
	}

	 @Override
	public int getFetchDirection() throws SQLException {
		return this.realStatement.getFetchDirection();
	}

	 @Override
	public void setFetchSize(int rows) throws SQLException {
		this.realStatement.setFetchSize(rows);
	}

	 @Override
	public int getFetchSize() throws SQLException {
		return this.realStatement.getFetchSize();
	}

	 @Override
	public int getResultSetConcurrency() throws SQLException {
		return this.realStatement.getResultSetConcurrency();
	}

	 @Override
	public int getResultSetType() throws SQLException {
		return this.realStatement.getResultSetType();
	}

	 @Override
	public Connection getConnection() throws SQLException {
		return this.realStatement.getConnection();
	}

	 @Override
	public boolean getMoreResults(int current) throws SQLException {
		return this.realStatement.getMoreResults();
	}

	 @Override
	public ResultSet getGeneratedKeys() throws SQLException {
		return this.realStatement.getGeneratedKeys();
	}

	 @Override
	public int getResultSetHoldability() throws SQLException {
		return this.realStatement.getResultSetHoldability();
	}

	 @Override
	public boolean isClosed() throws SQLException {
		return this.realStatement.isClosed();
	}

	 @Override
	public void setPoolable(boolean poolable) throws SQLException {
		this.realStatement.setPoolable(poolable);
	}

	 @Override
	public boolean isPoolable() throws SQLException {
		return this.realStatement.isPoolable();
	}

	 @Override
	public <T> T unwrap(Class<T> iface) throws SQLException {
		return this.realStatement.unwrap(iface);
	}

	 @Override
	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		return this.realStatement.isWrapperFor(iface);
	}

	 @Override
	public void closeOnCompletion() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	 @Override
	public boolean isCloseOnCompletion() throws SQLException {
		throw new UnsupportedOperationException("Not supported yet.");
	}
}
